package com.genghis.rapidcrm;

import com.genghis.rapidcrm.web.interceptor.RapidViewInterceptor;
import com.genghis.rapidcrm.web.view.language.RapidMultipleMessageSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.ThemeResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.theme.CookieThemeResolver;

/**
 * @author xulixin
 */
@Configuration
public class RapidAppConfig implements WebMvcConfigurer {

    @Value("${rapidcrm.skin.name:softed}")
    private String skinName;

    private final RapidViewInterceptor rapidViewInterceptor;

    public RapidAppConfig(RapidViewInterceptor rapidViewInterceptor) {
        this.rapidViewInterceptor = rapidViewInterceptor;
    }

    @Override
    public void addInterceptors(final InterceptorRegistry registry) {
        registry.addInterceptor(rapidViewInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("login", "/logout", "/error/**");
    }

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new RapidMultipleMessageSource();
        messageSource.setBasename("classpath*:i18n/messages");
        messageSource.setCacheSeconds(-1);
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Bean
    public ThemeResolver themeResolver() {
        CookieThemeResolver themeResolver = new CookieThemeResolver();
        themeResolver.setDefaultThemeName(skinName);
        return themeResolver;
    }

}
